package com.hmsm.server.base;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hmsm.server.common.AllContext;
import com.hmsm.sys.bean.LocalRole;
import com.hmsm.sys.bean.LocalUser;
import com.hmsm.sys.service.IRoleInfoService;
import com.hmsm.sys.service.IUserInfoService;

public class BaseService<T, ID extends Serializable, U extends LocalUser<?, ?, ?>, R extends LocalRole> {
	
	public static final String RIGHT_SYS_GROUP  		= "SystemManage";	// 系统管理组权限
	
	public static final String RIGHT_ORG_MANAGE  		= "1001";		// 组织机构管理权限
	public static final String RIGHT_USER_MANAGE 		= "1002";		// 用户管理权限
	public static final String RIGHT_ROLE_MANAGE 		= "1003"; 		// 角色及权限管理
	public static final String RIGHT_ADMIN_MANAGE 		= "1004"; 		// 管理员管理 
	public static final String RIGHT_LOG_MANAGE 		= "1005"; 		// 日志管理 
	public static final String RIGHT_MESSAGE_MANAGE 	= "1006"; 		// 消息管理 
	public static final String RIGHT_DEPT_GROUP_MANAGE = "1007"; 		// 业务部门分组管理
	public static final String RIGHT_SYS_MANAGE 		= "1008"; 		// 系统管理员权限
	
	public static final String RIGHT_RANGE_ORG_ALL = "OrgRangeAll";				// 组织机构权限范围标识 所有的
	public static final String RIGHT_RANGE_ORG_ORG = "OrgRangeOrg";				// 组织机构权限范围标识 组织机构的
	public static final String RIGHT_RANGE_ORG_DEPT = "OrgRangeDetp";			// 组织机构权限范围标识 部门的
	public static final String RIGHT_RANGE_ORG_NONE = "OrgRangeNone";			// 组织机构权限范围标识 无
	
	public static final String ADMIN_ROLE_NAME = "系统管理员";
	public static final String SUPER_ADMIN_NAME = "超级管理员";
	
	@Resource
	private IUserInfoService<U, Serializable> userInfoService;
	
	public IUserInfoService<U, Serializable> getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(IUserInfoService<U, Serializable> userInfoService) {
		this.userInfoService = userInfoService;
	}

	@Resource
	private IRoleInfoService<R, Integer, U> roleInfoService;

	@SuppressWarnings("unchecked")
	public IRoleInfoService<R, Integer, U> getRoleInfoService() {
		if (roleInfoService==null){
			roleInfoService = (IRoleInfoService<R, Integer, U>) AllContext.getBeanService(IRoleInfoService.class);
		}
		return roleInfoService;
	}

	public void setRoleInfoService(IRoleInfoService<R, Integer, U> roleInfoService) {
		this.roleInfoService = roleInfoService;
	}

	// 日志子类
	private static final Log log = LogFactory.getLog(BaseService.class);	
	
	private static String version = "1.0.0.0";
	
	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		BaseService.version = version;
	}

	protected IBaseDao<T, ID> baseDao;

	public IBaseDao<T, ID> getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(IBaseDao<T, ID> baseDao) {
		this.baseDao = baseDao;

	}

	@SuppressWarnings("unchecked")
	public com.lzyyj.gmis.base.dao.IBaseDao<T, ID> getLzyyjBaseDao() {
		com.lzyyj.gmis.base.dao.IBaseDao<Object, Integer> lzyyjBaseDao = (com.lzyyj.gmis.base.dao.IBaseDao<Object, Integer>) AllContext.getBean("HibernateEntityDao");
		return (com.lzyyj.gmis.base.dao.IBaseDao<T, ID>) lzyyjBaseDao.getBaseDao(baseDao.getClass());
	}

	public boolean log(Object obj, String message) {
		log.info("<<" + obj.getClass() + ":" + message + ">>");
		return true;
	}

	public boolean error(Object obj, String message) {
		log.error("<<" + obj.getClass() + ":" + message + ">>");
		return true;
	}

	/**
	 * 
	 *  @描述:保存对象信息
	 *  @方法名称:save
	 *  @作者:yaosq
	 *  @创建日期:2014-4-1 下午04:05:51 
	 *  @param object
	 *  @return
	 * @throws Exception 
	 *
	 */
	public T save(T object) throws Exception {
		baseDao.save(object);
		return object;
	}

	/**
	 * 
	 *  @描述:更新对象信息
	 *  @方法名称:update
	 *  @作者:yaosq
	 *  @创建日期:2014-4-1 下午04:06:27 
	 *  @param object
	 *  @return
	 * @throws Exception 
	 *
	 */
	public T update(T object) throws Exception {
		baseDao.update(object);
		return object;
	}

	/**
	 * 
	 *  @描述:删除对象数据
	 *  @方法名称:delete
	 *  @作者:yaosq
	 *  @创建日期:2014-4-1 下午04:06:42 
	 *  @param object
	 *  @return
	 * @throws Exception 
	 *
	 */
	public boolean delete(T object) throws Exception {
		baseDao.delete(object);
		return true;
	}

	/**
	 * 
	 *  @描述:根据ID获取对象
	 *  @方法名称:getById
	 *  @作者:yaosq
	 *  @创建日期:2014-4-1 下午04:06:53 
	 *  @param id
	 *  @return
	 *
	 */
	public T getById(ID id) {
		return baseDao.getById(id);
	}

	/**
	 * 判断对象某些属性的值在数据库中是否唯一.
	 * @param propertyList 不能重复的属性列表,以逗号分割 如"name,loginid,password"
	 */
	public boolean isUnique(T entityVo, String propertyList) {
		return baseDao.isUnique(entityVo, propertyList);
	}
	
	public boolean haveRight(String code, U currentUser) throws Exception {
		return getRoleInfoService().haveRight(code, currentUser);
	}
	
	public boolean haveRightRange(U currentUser, String funcCode, String... rangeCodes) throws Exception {
		boolean result = false;
		
		IRoleInfoService<?, ?, U> roleInfoService = getRoleInfoService();
		if (roleInfoService!=null){
			result = roleInfoService.haveRightRange(currentUser, funcCode, rangeCodes);
		}
		return result;
	}

	/**
	 * 获取外键应用对象列表
	 * 
	 * @param objectClass
	 * @param aliasNames 别名列表，逗号分隔
	 * @param objId  主键
	 * @return
	 * @throws Exception 
	 */
	public Map<String, List<? extends Object>> getRefListMap(Class<?> objectClass, String aliasNames, Serializable objId) throws Exception {
		com.lzyyj.gmis.base.dao.IBaseDao<T, ID> lzyyjBaseDao = getLzyyjBaseDao();
		Map<String, List<? extends Object>> refObjectMap = lzyyjBaseDao.getRefListMap(objectClass, aliasNames, objId);
		return refObjectMap;
	}

	/**
	 * 替换外键引用
	 * 
	 * @param objectClass
	 * @param aliasName 别名
	 * @param srcId
	 * @param tarId
	 * @throws Exception 
	 */
	public int replaceRef(Class<?> objectClass, String aliasNames, Serializable srcId, Serializable tarId) throws Exception {
		int count = getLzyyjBaseDao().replaceRef(objectClass, aliasNames, srcId, tarId);	
		return count;
	}
	
}
